#ifndef XENO_CONTROL_MAPPER_HPP
#define XENO_CONTROL_MAPPER_HPP

namespace xeno_control
{
    template <typename T>
    class Mapper
    {
    private:
        double forwardSlope, forwardIntercept;
        double reverseSlope, reverseIntercept;
        double inputMin{}, inputMax{};
        T outputMin, outputMax;

    public:
        Mapper(const double inMin, const double inMax, T outMin, T outMax) : inputMin(inMin), inputMax(inMax),
                                                                             outputMin(outMin),
                                                                             outputMax(outMax)
        {
            // 正向映射：input -> output
            forwardSlope = (outputMax - outputMin) / (inputMax - inputMin);
            forwardIntercept = outputMin - forwardSlope * inputMin;

            // 反向映射：output -> input
            reverseSlope = (inputMax - inputMin) / (outputMax - outputMin);
            reverseIntercept = inputMin - reverseSlope * outputMin;
        }

        T forward(double input) const
        {
            input = std::clamp(input, inputMin, inputMax);

            return forwardSlope * input + forwardIntercept;
        }

        [[nodiscard]] double reverse(double output) const
        {
            output = std::clamp(output, static_cast<double>(outputMin), static_cast<double>(outputMax));

            return reverseSlope * output + reverseIntercept;
        }
    };
}

#endif //XENO_CONTROL_MAPPER_HPP
